<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('出库管理列表')" />
    <th:block th:include="include :: bootstrap-editable-css" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row ">
            <div class="col-sm-12 hidden search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li hidden="hidden">
                                <label>销售单号：</label>
                                <input type="text" name="orderNo"/>
                            </li>
                            <li hidden="hidden" >
                                <label>产品id：</label>
                                <input type="text" name="productId"/>
                            </li>
                            <li hidden="hidden">
                                <label>供应商ID：</label>
                                <input type="text" name="supplierId"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm " id="toolbar" role="group">
                <a class="btn btn-success hidden" onclick="$.operate.add()" shiro:hasPermission="warehouse:orderDetail:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="openEdit()" shiro:hasPermission="warehouse:orderDetail:edit">
                    <i class="fa fa-edit"></i> 出库
                </a>
                <a class="btn btn-danger multiple disabled " onclick="$.operate.removeAll()" shiro:hasPermission="warehouse:orderDetail:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning " onclick="$.table.exportExcel()" shiro:hasPermission="warehouse:orderDetail:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table-store"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <th:block th:include="include :: bootstrap-table-editable-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('warehouse:orderDetail:edit')}]];
        var removeFlag = [[${@permission.hasPermi('warehouse:orderDetail:remove')}]];
        var statusDatas = [[${@dict.getType('b_order_detail_status')}]];
        var prefix = ctx + "contract/contractProduct";
        let contractId=[[${contractId}]];
        let orderId=[[${orderId}]];
        let contractType=[[${contractType}]];
        let roleFlag=[[${roleFlag}]];
        let chooseCheckBox=[];

        let url=prefix+"/list?";
        if(!isEmptyString(contractId)){
            url=url+"&contractId="+contractId;
            $(".btn-primary").addClass("hidden");

        }
        if(!isEmptyString(contractType)){
            url=url+"type="+contractType;
            hiddeInfo();
        }
        function isShowOperationNumFiled(){
            if(roleFlag==roleObj.supplierRole){//供应商
                hiddeInfo()
                return false;
            }else{//租户
                return  !isEmptyString(contractId);
            }
        }
        function hiddeInfo(){
            $(".info").addClass("hidden");
        }

        $(function() {
            var options = {
                id:"bootstrap-table-store",
                url: url,
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                onEditableSave: onEditableSave,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sortName: "contractProductId",
                sortOrder:"desc",
                modalName: "出库",
                onCheck: onCheck,
                onUncheck: onUncheck,
                onCheckAll: onCheckAll,
                onUncheckAll: onUncheckAll,
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'contractProductId',
                        title: 'ID',
                        visible: false
                    },
                    {
                        field: 'contractId',
                        title: '合同Id',
                        visible: false
                    },
                {
                    field: 'name',
                    title: '产品名称'
                },
                {
                    field: 'code',
                    title: '产品编码',
                },
                {
                    field: 'unit',
                    title: '单位'
                },
                {
                    field: 'price',
                    title: '单价'
                },
                {
                    field: 'storeNum',
                    title: '库存数量',
                    visible:false
                },
                {
                    field: 'num',
                    title: '销售数量'
                },
                {
                    field: 'useNum',
                    title: '已出库数量',
                },
                {
                    field: 'remainNum',
                    title: '未出库数量'
                },
                {
                    field: 'operationNum',
                    title: '本次出库数量',
                    visible: isShowOperationNumFiled(),
                    editable : {
                        type : 'text',
                        title : '本次出库数量',
                        validate : function(value) {
                            if (value.length == 0) {
                                return '本次入库数量不能为空';
                            }
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(Number(row.useNum)==0){
                            actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openSellDetail(\'' + row.contractProductId + '\')"><i class="fa fa-search1"></i>已经完成出库</a> ');
                        }else{
                            actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openSellDetail(\'' + row.contractProductId + '\')"><i class="fa fa-search1"></i>出库详情</a> ');

                        }
                        return actions.join('');
                    }
                }
                ]
            };
            $.table.init(options);
        });



        function onCheck(row, $element){
            chooseCheckBox.push(row);
           // alert("onCheck  选中行userId：" + row.userId + " userName：" + row.userName);
        }

        function onUncheck(row, $element){
            chooseCheckBox= chooseCheckBox.filter(item=>item !=row)
           // alert("onUncheck 取消行userId：" + row.userId + " userName：" + row.userName);
        }

        function onCheckAll(rowsAfter, rowsBefore){
            // var rows = $.map(rowsAfter, function(row) {
            //     return $.common.getItemField(row, "userId");
            // });
            chooseCheckBox=rowsAfter;
           // alert("全选行：" + rows);
        }

        function onUncheckAll(rowsAfter, rowsBefore){
           /* var rows = $.map(rowsBefore, function(row) {
                return $.common.getItemField(row, "userId");
            });*/
            chooseCheckBox=[];
           // alert("取消行：" + rows);
        }

        function openEdit(){
            var data = $("#bootstrap-table-store").bootstrapTable('getSelections');
            var id=data[0].orderDetailId;
            $.modal.open("出库",ctx+"warehouse/orderDetail/edit/"+id+"/"+contractType,1200,($(window).height() - 50),function (index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler(index, layero);
                setTimeout(function () {
                    $.table.refresh();
                },800);

            });
        }
        function openSellDetail(orderDetailId){
            openWindOptions("查看出库详情",ctx+"warehouse/sellStoreDetail?orderDetailId="+orderDetailId)
        }

        function onEditableSave (field, row, rowIndex, oldValue, $el) {
            //alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
            if(field=="operationNum"){
                if(Number(row.operationNum)>Number(row.remainNum)){
                    $.modal.alertWarning("【"+row.name+"】本次出库数量不能大于剩余数量");
                    return;
                }
                if(Number(row.operationNum)>Number(row.storeNum)){
                    $.modal.alertWarning("【"+row.name+"】本次出库数量不能大于库存数量");
                    return;
                }
            }
        }

        function getAllStoreData(){
            var arr=[];
            var data = chooseCheckBox;//$("#bootstrap-table-store").bootstrapTable('getSelections');
            if(data.length==0){
                $.modal.alertWarning("请选择出库的产品");
                return;
            }
            for(var i=0;i<data.length;i++){
                var row=data[i];
                if(row.operationNum==null||row.operationNum==""||Number(row.operationNum)==0){
                    continue;
                }else if(Number(row.operationNum)>Number(row.remainNum) ){
                    $.modal.alertWarning("【"+row.name+"】本次出库数量不能大于剩余数量");
                    return;

                }else if(Number(row.operationNum)>Number(row.storeNum)){
                    $.modal.alertWarning("【"+row.name+"】本次出库数量不能大于库存数量");
                    return;
                }else{
                    arr.push(data[i]);
                }
            }
            return JSON.stringify(arr);
        }

        function getContractId(){
            return contractId;
        }
    </script>
</body>
</html>
